package com.mine.yyb.modules.sys.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.web.YybBaseController;
import com.mine.yyb.modules.sys.bo.NoticeBO;
import com.mine.yyb.modules.sys.enums.NoticeStatusEnum;
import com.mine.yyb.modules.sys.enums.SysCodeEnum;
import com.mine.yyb.modules.sys.service.INoticeService;
import com.mine.yyb.modules.sys.vo.NoticeVO;

/**
 * <pre>
 * <一句话功能简述>
 * 推送内容
 * <功能详细描述>
 * </pre>
 * 
 * @author xuganhua
 * @version [版本号, 2016年02月15日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping(value = "/sys/notice")
public class NoticeControl extends YybBaseController
{
    
    @Autowired
    private INoticeService iNoticeService;
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 保存推送内容信息
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年12月13日]
     * @param request
     * @param userBO
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/saveNotice")
    public ResponseEntity<String> saveNotice(HttpServletRequest request, @ModelAttribute NoticeBO noticeBO)
    {
        // 响应码
        String retCode = null;
        try
        {
            logger.info(noticeBO.toString());
            
            // 判断推送内容为空
            if (StringUtils.isBlank(noticeBO.getContent()))
            {
                throw new MineException(SysCodeEnum.ERROR_CONTENT_BLANK);
            }
            
            retCode = iNoticeService.saveNotice(noticeBO);
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 更新推送信息
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年12月13日]
     * @param request
     * @param userBO
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/updateNotice")
    public ResponseEntity<String> updateNotice(HttpServletRequest request, @ModelAttribute NoticeBO noticeBO)
    {
        String retCode = null;
        try
        {
            logger.info(noticeBO.toString());
            
            retCode = iNoticeService.updateNotice(noticeBO);
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode);
    }
    
    /**
     * <一句话功能简述>
     * 
     * 删除推送内容信息
     *
     * <功能详细描述>
     * 
     * @param request
     * @param userBO
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/deleteNotice")
    public ResponseEntity<String> deleteNotice(HttpServletRequest request, @ModelAttribute NoticeBO noticeBO)
    {
        String retCode = null;
        try
        {
            logger.info(noticeBO.toString());
            
            retCode = iNoticeService.deleteNotice(noticeBO);
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 查询需要推送的推送内容
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年02月15日]
     * @param request
     * @param noticeBO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/queryTheNotice")
    public ResponseEntity<String> queryTheNotice(HttpServletRequest request, @ModelAttribute NoticeBO noticeBO)
        throws MineException
    {
        String retCode = null;
        NoticeVO noticeVO = null;
        try
        {
            logger.info(noticeBO.toString());
            
            noticeBO.setStatus(NoticeStatusEnum.NOTICE_RELEASE_STATUS.getStatus());
            noticeVO = iNoticeService.queryNotice(noticeBO);
            retCode = MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        return getResponseEntity(retCode, noticeVO);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 查询推送内容详细信息
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月4日]
     * @param request
     * @param userBO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/queryNotice")
    public ResponseEntity<String> queryNotice(HttpServletRequest request, @ModelAttribute NoticeBO noticeBO)
        throws MineException
    {
        String retCode = null;
        NoticeVO noticeVO = null;
        try
        {
            logger.info(noticeBO.toString());
            
            noticeVO = iNoticeService.queryNotice(noticeBO);
            if(noticeVO == null)
            {
                noticeVO = new NoticeVO();
            }
            retCode = MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode, noticeVO);
    }
    
}
